package com.ruoyi.collegeClass.controller;

import java.util.List;

import java.util.Set;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;

import com.ruoyi.system.api.domain.SysUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.collegeClass.domain.BwClass;
import com.ruoyi.collegeClass.service.IBwClassService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@RestController
@RequestMapping("/collegeClass")
public class BwClassController extends BaseController
{
    @Autowired
    private IBwClassService bwClassService;

    /**
     * 查询班级列表
     */
    @RequiresPermissions("collegeClass:collegeClass:list")
    @GetMapping("/list")
    public TableDataInfo list(BwClass bwClass)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        String roleName = null;
        for (String role : roles) {
            if (!role.isEmpty()){
                roleName = role;
                if (role.equals("manager")){
                    bwClass.setManageId(userId);
                }else if (role.equals("guide")){
                    bwClass.setGuidanceId(userId);
                }else if (role.equals("charge")){
                    bwClass.setChargeId(userId);
                }
            }
        }
        List<BwClass> list = bwClassService.selectBwClassList(bwClass);
        for (BwClass aClass : list) {
            Long schoolId = aClass.getSchoolId();
            SysDept sysDept = bwClassService.selectDeptById(schoolId);
            String deptName = sysDept.getDeptName();
            aClass.setSchoolName(deptName);
            Long collegeId = aClass.getCollegeId();
            SysDept sysDept1 = bwClassService.selectDeptById(collegeId);
            String deptName1 = sysDept1.getDeptName();
            aClass.setCollegeName(deptName1);

            Long manageId = aClass.getManageId();
            SysUser sysUser = bwClassService.selectUserById(manageId);
            aClass.setManageName(sysUser.getNickName());
            Long guidanceId = aClass.getGuidanceId();
            SysUser sysUser2 = bwClassService.selectUserById(guidanceId);
            aClass.setGuidanceName(sysUser2.getNickName());
            Long chargeId = aClass.getChargeId();
            SysUser sysUser1 = bwClassService.selectUserById(chargeId);
            aClass.setChargeName(sysUser1.getNickName());

        }
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @RequiresPermissions("collegeClass:collegeClass:export")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BwClass bwClass)
    {
        List<BwClass> list = bwClassService.selectBwClassList(bwClass);
        ExcelUtil<BwClass> util = new ExcelUtil<BwClass>(BwClass.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @RequiresPermissions("collegeClass:collegeClass:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bwClassService.selectBwClassById(id));
    }

    /**
     * 新增班级
     */
    @RequiresPermissions("collegeClass:collegeClass:add")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
        public AjaxResult add(@RequestBody BwClass bwClass)
    {
        if (bwClassService.insertBwClass(bwClass)>0){
        return toAjax(bwClassService.insertBwClass(bwClass));
        }else {
            return new AjaxResult(500,"该项目经理还带有非毕业班级,不可再次添加");
        }
    }

    /**
     * 修改班级
     */
    @RequiresPermissions("collegeClass:collegeClass:edit")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BwClass bwClass)
    {
        return toAjax(bwClassService.updateBwClass(bwClass));
    }

    /**
     * 删除班级
     */
    @RequiresPermissions("collegeClass:collegeClass:remove")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bwClassService.deleteBwClassByIds(ids));
    }

    /**
     * 三级联查查项目经理
     */
    @PostMapping("/collegeTeacher")
    public R collegeTeacher()
    {
        return bwClassService.collegeTeacher();
    }
    /**
     * 三级联查查就业指导
     */
    @PostMapping("/collegeDirection")
    public R collegeDirection()
    {
        return bwClassService.collegeDirection();
    }

    @PostMapping("/findDeptInfoByPid")
    public R findDeptInfoByPid(@RequestParam Long pid)
    {
        return bwClassService.findDeptInfoByPid(pid);
    }
}
